/**
 * Project Name:boar-base-jar 
 * File Name:DirBizAspect.java 
 * Date:2017年5月9日
 */
package com.boarsoft.boar.soagov.dir;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.boarsoft.boar.dir.DirExAct;
import com.boarsoft.boar.dir.DirExBiz;
import com.boarsoft.boar.entity.DirInfo;

/**
 * DirExBiz切面
 * 
 * @author YangLiu
 * @CreateDate 2017年5月9日 下午3:39:30
 * @version 1.0
 * 
 * @UpdateUser 修改人名称
 * @UpdateDate 2017年5月9日 下午3:39:30
 * @UpdateRemark 修改具体的内容
 */
@Component
@Aspect
public class DirBizExAspect {

	@Autowired
	private DirExAct dirExAct;
	
	@Autowired
	private DirExBiz dirExBiz;
	
	@Pointcut("execution(* com.boarsoft.boar.dir.biz.DirBizImpl.delete(com.boarsoft.boar.entity.DirInfo))")
	public void deleteDir() {
		
	}
	
	@After("deleteDir()")
	public void delete(JoinPoint jp) throws Exception {
		DirInfo dirInfo = (DirInfo) jp.getArgs()[0];
		dirExBiz.delete(dirInfo.getType(), dirInfo.getId());
		dirExAct.delete(dirInfo);
	}
	
	@Pointcut("execution(* com.boarsoft.boar.dir.biz.DirBizImpl.copy(com.boarsoft.boar.entity.DirInfo, String))")
	public void copyDir() {
		
	}
	
	@AfterReturning(value="copyDir()", returning="obj")
	public DirInfo copy(JoinPoint jp, Object obj) throws Exception {
		DirInfo newDir = (DirInfo) obj;
		DirInfo oldDir = (DirInfo) jp.getArgs()[0];
		String toParent = (String) jp.getArgs()[1];
		short dirType = oldDir.getType();
		if (DirInfo.TYPE_FACE == dirType
				|| DirInfo.TYPE_COM == dirType
				|| DirInfo.TYPE_SVC == dirType
				|| DirInfo.TYPE_FLOW == dirType) {
			dirExBiz.copy(dirType, oldDir.getId(), toParent, newDir.getId());
		}
		dirExAct.copy(oldDir, newDir);
		return newDir;
	}
	
	@Pointcut("execution(* com.boarsoft.boar.dir.biz.DirBizImpl.move(com.boarsoft.boar.entity.DirInfo, String))")
	public void moveDir() {
		
	}
	
	@Around(value="moveDir()")
	public boolean moveAction(ProceedingJoinPoint jp) throws Throwable {
		DirInfo dirInfo = (DirInfo) jp.getArgs()[0];
		DirInfo oldDir = new DirInfo();
		BeanUtils.copyProperties(dirInfo, oldDir);
		
		Object result = jp.proceed();
		
		if (((Boolean)result).booleanValue()) {
			dirExAct.move(oldDir, dirInfo);
		}
		return ((Boolean)result).booleanValue();
	}
}
